package com.ellecity06.cwhiskymall.ui.video

import android.os.Bundle
import android.view.SurfaceHolder
import android.view.View
import com.aliyun.player.AliPlayer
import com.aliyun.player.AliPlayerFactory
import com.aliyun.player.IPlayer
import com.aliyun.player.bean.ErrorInfo
import com.aliyun.player.source.UrlSource
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.base.BaseViewModel
import com.ellecity06.common.ktx.isFoldExpandStatus
import com.ellecity06.common.utils.appContext
import com.ellecity06.common.utils.logD
import com.ellecity06.common.widgets.drag.DragBackLayout
import com.ellecity06.cwhiskymall.databinding.ActivityAliVideoPlayBinding

/**
 * @author ellecity06
 * @time 2024/12/24 18:22
 * @des
 */
class AliPlayVideoActivity : BaseMvvmActivity<ActivityAliVideoPlayBinding, BaseViewModel>(),
    IPlayer.OnPreparedListener, IPlayer.OnRenderingStartListener, IPlayer.OnErrorListener, SurfaceHolder.Callback {
    private val videoUrl: String? by lazy { intent.getStringExtra("video_url") }
    override val userBaseTitle: Boolean
        get() = false
    private val mAliPlayer: AliPlayer by lazy {
        AliPlayerFactory.createAliPlayer(appContext).apply {
            isAutoPlay = true
            // 设置直播不缓存
            setOnRenderingStartListener(this@AliPlayVideoActivity)
            setOnPreparedListener(this@AliPlayVideoActivity)
            setOnErrorListener(this@AliPlayVideoActivity)
        }
    }

    override fun inflateBinding(): ActivityAliVideoPlayBinding {
        return ActivityAliVideoPlayBinding.inflate(layoutInflater)
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        DragBackLayout(this).attachToActivity(this, object : DragBackLayout.Callback() {
            override fun onBack() {
                finish()
                overridePendingTransition(0, 0)
            }
        })
    }

    override fun initialize(savedInstanceState: Bundle?) {
        logD("视频播放url=$videoUrl")
        binding.ivBack.setOnClickListener {
            onBackPressed()
        }
        binding.surfaceView.holder.addCallback(this)
        preparePlay(videoUrl)
    }

    override fun onResume() {
        mAliPlayer.start()
        super.onResume()
    }

    override fun onPause() {
        mAliPlayer.pause()
        super.onPause()
    }
    private fun preparePlay(pullUrl: String?) {
        if (pullUrl.isNullOrEmpty()) {
            return
        }
        val config = mAliPlayer.config
        config.mEnableLocalCache = true
        config.mMaxDelayTime = 1000
        config.mHighBufferDuration = 100
        config.mStartBufferDuration = 100
        config.mEnableSEI = true
        mAliPlayer.config = config
        if (!isFoldExpandStatus()) {
            mAliPlayer.scaleMode = IPlayer.ScaleMode.SCALE_ASPECT_FILL
        } else {
            mAliPlayer.scaleMode = IPlayer.ScaleMode.SCALE_ASPECT_FIT

        }
        val source = UrlSource()
        source.uri = pullUrl
        mAliPlayer.setDataSource(source)
        mAliPlayer.prepare()
        mAliPlayer.start()

    }

    override fun onDestroy() {
        super.onDestroy()
        mAliPlayer.stop()
        mAliPlayer.setSurface(null)
        mAliPlayer.release()
    }

    override fun onPrepared() {
        binding.surfaceView.visibility = View.VISIBLE
    }

    override fun onRenderingStart() {
    }

    override fun onError(p0: ErrorInfo?) {
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        mAliPlayer.setSurface(holder.surface)
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        mAliPlayer.surfaceChanged()
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        mAliPlayer.setSurface(null)
    }
}